//
//  MeViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "MeViewController.h"





@interface MeViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) NSMutableArray *caseDataSource;
@property (nonatomic, strong) UIButton *headImageButton;
@property (nonatomic, strong) UIImageView *headImage;
@property (nonatomic, strong) UILabel *nameLable;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSDictionary *myInfoDic;
@property (nonatomic, strong) UITableView *myInfoTableView;
@property (nonatomic, strong) UIView *barView;
@property (nonatomic, strong) UILabel *barTitleLable;
@end

@implementation MeViewController
- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
//    self.tabBarController.tabBar.hidden = NO;
    self.automaticallyAdjustsScrollViewInsets = false;
}
//- (void)viewWillDisappear:(BOOL)animated{
//    self.tabBarController.tabBar.hidden = YES;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"--------------%@",[InfoSingleTon personInfoSingleTon].infoDic);
    [self InitializeInterface];
    [self getInfo];
    self.view.backgroundColor = [UIColor whiteColor];
}
- (void)getInfo{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@id=%@",PERSONALINFO_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]] dic:nil Succeed:^(id responseData) {
        self.myInfoDic = responseData[@"result"][0];
        NSLog(@"success： %@ ",self.myInfoDic);
        [self.myInfoTableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(NSError *error) {
        NSLog(@"error： %@",error);
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (void)InitializeInterface{
    [self.view addSubview:self.barView];
    [self.barView addSubview:self.barTitleLable];
    [self.view addSubview:self.myInfoTableView];
}
#pragma mark - Button Target
- (void)action_Submit:(UIButton *)sender{
    NSLog(@"提交 ");
}
- (void)action_LogOut:(UIButton *)sender{
    NSLog(@"退出登录 ");
     [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginMessage"];
    LoginViewController *login = [[LoginViewController alloc]init];
    login.LogOut = 1;//等于1表示是用户退出登录
    [self presentViewController:login animated:NO completion:nil];
    //删除用户信息
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"loginMessage"];
    
    
    
    
    
    LoginViewController *logVC = (LoginViewController *) [UIApplication sharedApplication].keyWindow.rootViewController ;
    [UIApplication sharedApplication].keyWindow.rootViewController = logVC;
}
#pragma mark - UIImagePicker
- (void)pickHeadImage{
    UIAlertController *arlertVc = [UIAlertController alertControllerWithTitle:@"提示" message:nil preferredStyle:UIAlertControllerStyleActionSheet];//从下方弹出
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action2){
        [self chooseType:2];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action3){
        [self chooseType:3];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [arlertVc addAction:action1];
    [arlertVc addAction:action2];
    [arlertVc addAction:action3];
    [self presentViewController:arlertVc animated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info{
    _image = info[UIImagePickerControllerOriginalImage];
    _headImage.image = _image;
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)chooseType:(NSInteger)type{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;//代理
    imagePicker.editing = YES;
    if (type == 2) {
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self showViewController:imagePicker sender:nil];
    }
    if (type == 3) {
        [imagePicker dismissViewControllerAnimated:YES completion:nil];
    }
    else{
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] ) {
        //如果照相机能用，继续推送（防止模拟器崩溃）
    }
    [self showViewController:imagePicker sender:nil];
}


#pragma mark - UITableViewDelegate/UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.titleArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 120;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myInfoCell" forIndexPath:indexPath];
    cell.titleLable.text = self.titleArray[indexPath.row];
    if (indexPath.row == 0) {
        cell.inputInfo.text = self.myInfoDic[@"name"];
    }
    else if (indexPath.row == 1) {
        cell.inputInfo.text = self.myInfoDic[@"name1"];
    }
    else if (indexPath.row == 2) {
        cell.inputInfo.text = self.myInfoDic[@"email"];
    }
    else if (indexPath.row == 3) {
        cell.inputInfo.text = self.myInfoDic[@"mobile"];
    }
    else if (indexPath.row == 3) {
        cell.inputInfo.text = self.myInfoDic[@"remarks"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    NSLog(@"1");
//    //    CaseDetailViewController *caseDetailVC = [[CaseDetailViewController alloc] init];
//    //    [self.navigationController pushViewController:caseDetailVC animated:YES];
//}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *space = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
    
    UIButton *submitButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, self.view.bounds.size.width - 20, 50)];
    submitButton.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:189.0f/255.0f blue:240.0f/255.0f alpha:1.0f];
    [submitButton setTitle:@"提交 " forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(action_Submit:) forControlEvents:UIControlEventTouchUpInside];
    submitButton.layer.cornerRadius = 3;
    submitButton.layer.masksToBounds = YES;
    [space addSubview:submitButton];
    
    UIButton *logoutButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 70, self.view.bounds.size.width - 20, 50)];
    logoutButton.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:189.0f/255.0f blue:240.0f/255.0f alpha:1.0f];
    [logoutButton setTitle:@"退出登录 " forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(action_LogOut:) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.layer.cornerRadius = 3;
    logoutButton.layer.masksToBounds = YES;
    
    [space addSubview:submitButton];
    [space addSubview:logoutButton];
    return space;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *space = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
    space.backgroundColor = [UIColor colorWithRed:63.0f/255.0f green:189.0f/255.0f blue:240.0f/255.0f alpha:1.0f];
    
    [space addSubview:self.headImage];
    [space addSubview:self.nameLable];
    [space addSubview:self.headImageButton];
    return space;
}

#pragma mark - Getter
- (NSDictionary *)myInfoDic{
    if (!_myInfoDic) {
        _myInfoDic = [NSDictionary dictionary];
    }
    return _myInfoDic;
}
- (NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = [NSArray arrayWithObjects:@"    所属公司： ",@"    姓       名： ",@"    邮       箱： ",@"    中国 +86： ",@"    备       注： ", nil];
    }
    return _titleArray;
}
- (UIView *)barView{
    if (!_barView) {
        _barView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 64)];
        _barView.backgroundColor = [UIColor whiteColor];
    }
    return _barView;
}
- (UILabel *)barTitleLable{
    if (!_barTitleLable) {
        _barTitleLable = [[UILabel alloc]init];
        _barTitleLable.bounds = CGRectMake(0, 0, 200, 20);
        _barTitleLable.center = CGPointMake(self.barView.center.x, self.barView.center.y + 10);
        _barTitleLable.text = @"我的";
        _barTitleLable.textAlignment = NSTextAlignmentCenter;
        _barTitleLable.font = [UIFont systemFontOfSize:18];
        _barTitleLable.textColor = [UIColor blackColor];
        
    }
    return _barTitleLable;
}


- (UIButton *)headImageButton{
    if (!_headImageButton) {
        _headImageButton = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 60, 60)];
        _headImageButton.clipsToBounds = YES;
        _headImageButton.layer.cornerRadius = 30;
        _headImageButton.layer.borderColor = [UIColor whiteColor].CGColor;
        _headImageButton.layer.borderWidth = 2;
        [_headImageButton addTarget:self action:@selector(pickHeadImage) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headImageButton;
}
- (UILabel *)nameLable{
    if (!_nameLable) {
        _nameLable = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, 200, 50)];
        _nameLable.text = @"Lynn";//传值
    }
    return _nameLable;
}
- (UIImageView *)headImage{
    if (!_headImage) {
        _headImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
        _headImage.layer.cornerRadius = 30;
        _headImage.layer.masksToBounds = YES;
        _headImage.layer.borderWidth = 1;
        _headImage.image = [UIImage imageNamed:@"头像"];
        _headImage.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    return _headImage;
}


- (UITableView *)myInfoTableView{
    if (!_myInfoTableView) {
        _myInfoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH , SCREEN_HEIGHT ) style:UITableViewStyleGrouped];
        _myInfoTableView.backgroundColor = [UIColor whiteColor];
        _myInfoTableView.delegate = self;
        _myInfoTableView.dataSource = self;
        _myInfoTableView.estimatedRowHeight = 50;
        _myInfoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myInfoTableView registerClass:[MyInfoCell class] forCellReuseIdentifier:@"myInfoCell"];
    }
    return _myInfoTableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
